import { Subject } from 'rxjs'
import { map } from 'rxjs/operators'
import * as express from 'express'
import { createExpress } from '../../framework'

export interface MeepoOptions {
  req: express.Request
  res: express.Response
}
export class Meepo extends Subject<MeepoOptions> {
  constructor() {
    super()
  }
}

export function meepoBootstrap(port: number, host?: string): Meepo {
  const meepo = new Meepo()
  createExpress(port, host)
    .pipe(
      map(data => {
        const req = data.req
        const res = data.res
        return {
          req,
          res
        }
      })
    )
    .subscribe(res => meepo.next(res))
  return meepo
}
